package com.hu.demo02;

public class ThreadStop implements Runnable{
    private boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while (flag){
            System.out.println("run_Thread-->"+i++);
        }
    }
    // 设置公开的方法用来切换标志位
    public void stop(){
        this.flag=false;
    }
    public static void main(String[] args) {
        ThreadStop threadStop = new ThreadStop();
        new Thread(threadStop,"main").start();
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            if (i==50){
                // 调用stop()方法切换标志位
                threadStop.stop();
                System.out.println("线程停止了");
            }
        }
    }
}
